#ifndef CONNECTIONMANAGER_H
#define CONNECTIONMANAGER_H

#include <QObject>
#include <QString>
#include <QList>
#include <QMap>
#include <QSettings>
#include <QDateTime>
#include "sshmanager.h"

/**
 * @brief 连接管理器
 *
 * 管理SSH连接配置、历史记录和快速连接
 * 提供连接配置的持久化存储
 */
class ConnectionManager : public QObject
{
    Q_OBJECT

public:
    struct ConnectionProfile {
        QString id;
        QString name;
        QString host;
        int port;
        QString username;
        QString password; // 加密存储
        QString privateKeyPath;
        QString passphrase; // 加密存储
        QString description;
        QString group;
        QDateTime lastConnected;
        int connectCount;
        bool isFavorite;

        ConnectionProfile() : port(22), connectCount(0), isFavorite(false) {}
    };

    struct ConnectionHistory {
        QString profileId;
        QString profileName;
        QDateTime connectedTime;
        QDateTime disconnectedTime;
        QString duration;
        bool successful;
        QString errorMessage;
    };

    explicit ConnectionManager(QObject *parent = nullptr);
    ~ConnectionManager();

    // 配置文件管理
    bool saveProfile(const ConnectionProfile &profile);
    bool updateProfile(const ConnectionProfile &profile);
    bool deleteProfile(const QString &profileId);
    ConnectionProfile getProfile(const QString &profileId) const;
    QList<ConnectionProfile> getAllProfiles() const;
    QList<ConnectionProfile> getProfilesByGroup(const QString &group) const;
    QList<ConnectionProfile> getFavoriteProfiles() const;
    QList<ConnectionProfile> getRecentProfiles(int limit = 10) const;

    // 快速连接
    QString quickConnect(const QString &host, int port, const QString &username, const QString &password = "");
    ConnectionProfile createQuickProfile(const QString &host, int port, const QString &username, const QString &password = "");

    // 组管理
    QStringList getGroups() const;
    bool createGroup(const QString &group);
    bool renameGroup(const QString &oldGroup, const QString &newGroup);
    bool deleteGroup(const QString &group);
    bool addToGroup(const QString &profileId, const QString &group);
    bool removeFromGroup(const QString &profileId);

    // 搜索
    QList<ConnectionProfile> searchProfiles(const QString &keyword) const;
    QList<ConnectionProfile> searchProfiles(const QString &host, const QString &username) const;

    // 导入/导出
    bool importProfiles(const QString &filePath);
    bool exportProfiles(const QString &filePath, const QList<QString> &profileIds = QList<QString>());
    bool exportProfileAsJson(const QString &profileId, const QString &filePath);

    // 历史记录
    void addToHistory(const ConnectionHistory &history);
    QList<ConnectionHistory> getConnectionHistory(const QString &profileId = QString()) const;
    QList<ConnectionHistory> getRecentHistory(int limit = 50) const;
    bool clearHistory(const QString &profileId = QString());
    bool clearAllHistory();

    // 统计信息
    QMap<QString, QVariant> getStatistics() const;
    int getTotalConnections() const;
    int getSuccessfulConnections() const;
    QString getMostUsedHost() const;
    QMap<QString, int> getConnectionStatsByDay() const;

    // 加密/解密
    QString encryptPassword(const QString &password) const;
    QString decryptPassword(const QString &encrypted) const;

    // 验证
    bool validateProfile(const ConnectionProfile &profile) const;
    bool validateHost(const QString &host) const;
    bool validatePort(int port) const;
    bool validateUsername(const QString &username) const;
    //bool validateBasicSettings() const;

signals:
    void profileAdded(const ConnectionProfile &profile);
    void profileUpdated(const ConnectionProfile &profile);
    void profileDeleted(const QString &profileId);
    void historyAdded(const ConnectionHistory &history);
    void settingsChanged();

public slots:
    void loadSettings();
    void saveSettings();
    void resetSettings();

private:
    QSettings *m_settings;
    QMap<QString, ConnectionProfile> m_profiles;
    QList<ConnectionHistory> m_history;

    // 加密密钥
    QByteArray m_encryptionKey;

    // 内部方法
    QString generateProfileId() const;
    QString settingsKey(const QString &key) const;
    ConnectionProfile profileFromSettings(const QString &profileId) const;
    void profileToSettings(const ConnectionProfile &profile);
    QString serializeProfile(const ConnectionProfile &profile) const;
    ConnectionProfile deserializeProfile(const QString &data) const;

    // 历史记录管理
    void cleanupOldHistory();
    QString formatDuration(const QDateTime &start, const QDateTime &end) const;

    // 验证方法
    bool isValidHostAddress(const QString &host) const;
    bool isValidPortNumber(int port) const;
    bool isValidUsername(const QString &username) const;
};

#endif // CONNECTIONMANAGER_H
